package com.kalix.jyksh.record.biz;

import com.kalix.framework.core.api.persistence.JsonData;
import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalixjyksh.record.api.biz.IColumnRecordBeanService;
import com.kalix.ksh.record.api.dao.IColumnRecordBeanDao;
import com.kalix.ksh.record.api.model.RecordData;
import com.kalix.ksh.record.api.model.RecordMdl;
import com.kalix.ksh.record.entities.ColumnRecordBean;
import com.kalix.ksh.record.entities.LineRecordBean;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by Wanghw on 2018/10/12.
 * 柱状图数据
 */
public class ColumnRecordServiceImpl extends GenericBizServiceImpl<IColumnRecordBeanDao, ColumnRecordBean> implements IColumnRecordBeanService {
    public ColumnRecordServiceImpl(){
        super.init(ColumnRecordBean.class.getName());
    }

    public RecordData getAllColumncord() {
        JsonData jd = getAllEntityByQuery(1, 10000, "", "");
        List<ColumnRecordBean> datas = jd.getData();
        Map<String, List<ColumnRecordBean>> maps = new LinkedHashMap<>();

        List<String> names = new ArrayList<>();
        for (ColumnRecordBean cr : datas) {
            if (!names.contains(cr.getName())){
                names.add(cr.getName());
            }
        }

        RecordData rd = new RecordData();
        List<RecordMdl> rds =new ArrayList<>();

        for (String name : names){
            List<ColumnRecordBean> nDatas = datas.stream().filter((n)->name.equals(n.getName())).collect(Collectors.toList());
            nDatas.sort((d1, d2)->d1.getCreationDate().compareTo(d2.getCreationDate()));
            RecordMdl rm = new RecordMdl();
            rm.setName(name);
            rm.setData(nDatas);
            rds.add(rm);
        }
        rd.setData(rds);

        return rd;
    }
}
